package com.bank.manage.utils.sign;

import org.gavin.core.utils.GMSM4Utils;
import org.springframework.util.ObjectUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Random;

public class SignMd5Util {

    public static final String lents = "8";

    public static final String star = "0";

    public static final String end = "8";


    public static SignMd5VO getEncode(String timeMd5) {
        String newtimeMd5 = null;
        long nanoTime = System.nanoTime();
        int len = Integer.parseInt(lents);
        String str = getStr(len);
        StringBuffer sb = new StringBuffer();
        sb.append(nanoTime);
        int starts = Integer.parseInt(star);
        int ends = Integer.parseInt(end);
        if (!(starts <= str.length()) && !(ends >= str.length()) && starts < ends) {
            sb.append(str.substring(starts, ends));
        }
        newtimeMd5 = getMD5(sb.toString());
        if (ObjectUtils.isEmpty(timeMd5)) {
            timeMd5 = newtimeMd5;
        }
        String oldidmd5 = getMD5(timeMd5);
        String newidmd5 = getMD5(newtimeMd5);

        String encodemd5 = getMD5(newidmd5 + newtimeMd5);

        String decodeMd5 = getMD5(oldidmd5 + timeMd5);

        SignMd5VO signMd5VO = new SignMd5VO();
        signMd5VO.setDecodeStr(decodeMd5);
        signMd5VO.setExchangeStr(newtimeMd5);
        signMd5VO.setEncodeStr(encodemd5);
        return signMd5VO;
    }

    public static SignMd5VO getEncodeMast() {
        String newtimeMd5 = null;
        long nanoTime = System.nanoTime();
        int len = Integer.parseInt(lents);
        String str = getStr(len);
        StringBuffer sb = new StringBuffer();
        sb.append(nanoTime);
        int starts = Integer.parseInt(star);
        int ends = Integer.parseInt(end);
        if (!(starts <= str.length()) && !(ends >= str.length()) && starts < ends) {
            sb.append(str.substring(starts, ends));
        }
        newtimeMd5 = getMD5(sb.toString());

        String timeMd5 = newtimeMd5;
        String oldidmd5 = getMD5(timeMd5);
        String newidmd5 = getMD5(newtimeMd5);

        String encodemd5 = getMD5(newidmd5 + newtimeMd5);

        String decodeMd5 = getMD5(oldidmd5 + timeMd5);

        SignMd5VO signMd5VO = new SignMd5VO();
        signMd5VO.setDecodeStr(decodeMd5);
        signMd5VO.setExchangeStr(newtimeMd5);
        signMd5VO.setEncodeStr(encodemd5);
        return signMd5VO;
    }

    public static String getStr() {
        String str = new String();

        Random r = new Random();
        for (int i = 0; i < 5; i++) {
            int temp = r.nextInt(58) + 65;

            if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <= 'z')) {
                str += (char) temp;
            } else {
                i--;
            }
        }
        return str;
    }

    public static String getStr(Integer num) {
        String str = new String();

        Random r = new Random();
        for (int i = 0; i < num; i++) {
            int temp = r.nextInt(58) + 65;

            if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <= 'z')) {
                str += (char) temp;
            } else {
                i--;
            }
        }
        return str;
    }

    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            String md5Str = new BigInteger(1, md.digest()).toString(16);
            for (int i = 32 - md5Str.length(); i > 0; i--) {
                md5Str = "0" + md5Str;
            }
            return md5Str;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        SignMd5VO encode = getEncode("9b3f9e3b7f1b75a8bb4d82aced07d8e8");
        String srt = "67a033f027e3159cf07d26ef29a877c24954a81c46f585fc5e3e1e13107ca0f56e9d6b2e61824dce88177c06837af134";
        String decodeStr = encode.getDecodeStr();
        String s = GMSM4Utils.decryptEcb(srt, decodeStr);
        System.out.println(s);

    }
}
